﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.Text.RegularExpressions;
using System.Collections;

namespace FormsAppT8
{
    public partial class BQFROMSC: Form
    {

        //dbconn db = new dbconn();

        public BQFROMSC()
        {
            InitializeComponent();
            //comboBox1.SelectedIndex=0;

        }
      
        //清单界面绑定
        public void bdmain()
        {
            bqzk();
            string sql = "";
            string table = "";
            string x1 = textBox1.Text;
           // string x2 = textBox12.Text;
            string x3 = textBox8.Text;
            string x4 = textBox7.Text;
            string x5 = textBox10.Text;

            sql = "select distinct top 100 a.billno as 单据编号,a.billdate as 单据日期 from ppProduceStoreInMainInfo a,ppProduceStoreindetailinfo b,comMaterialGroup d where a.billno=b.billno and b.materialid=d.materialid " +
            " and a.billno like '%" + x1 + "%'" +
            " and d.materialid  like '%" + x3 + "%'"+
             " and d.materialname  like '%" + x4 + "%'"+
             " and d.cu_th  like '%" + textBox3.Text + "%'"+
             " and d.materialspec  like '%" + x5 + "%' order by billdate desc";

                table = "ppProduceStoreInMainInfo";
        
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            dbconn db = new dbconn();
            dataGridView1.Rows.Clear();
            db.bdlm(sql, table, dataGridView1);
            dataGridView2.Rows.Clear();
            dataGridView2.Columns.Clear();
        }

        //明细界面绑定
        public void bddetail()
        {
            bqzk();
            string sql = "";
            string table = "";
            string x1 = textBox1.Text;
           // string x2 = textBox12.Text;
            string x3 = textBox8.Text;
            string x4 = textBox7.Text;
            string x5 = textBox10.Text;
            string billno = "";
            if (dataGridView1.SelectedRows.Count > 0)
            {

                billno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            }
            sql = "select a.RowNo as 行号,a.MaterialId as 产品编号,b.MaterialName as 产品名称,b.Materialspec as 物料规格,cast( a.pQuantity as float) as 本次入库数量,a.pUnitId as 单位,b.cu_th as 图号 from (" +
               "select a.billno,a.RowNo,a.MaterialId,a.PQuantity,a.pUnitId from ppProduceStoreindetailinfo a " +
               ")a,comMaterialGroup b where a.MaterialId=b.MaterialId  " +
               "and a.BillNo='" + billno + "' and b.MaterialId  like '%" + x3 + "%'" + " and b.materialname  like '%" + x4 + "%'" +
             " and b.materialspec  like '%" + x5 + "%' and b.cu_th  like '%" + textBox3.Text + "%'";
            table = "ppProduceStoreindetailinfo";
                dataGridView2.Rows.Clear();
                dataGridView2.Columns.Clear();
                dbconn db = new dbconn();
                dataGridView2.Rows.Clear();
                db.bdlm(sql, table, dataGridView2);
        }

        //二维码生成
        public void ewmsc()
        {

            string data = textBox11.Text+"|"+textBox15.Text+"|";
                //pid + bh + "|" + textBox15.Text + "|" + textBox4.Text + "|" + textBox16.Text + "|" + textBox2.Text + "|" +( Convert.ToDouble(textBox5.Text)*1000).ToString();
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //设置编码模式 
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //设置编码测量度
            qrCodeEncoder.QRCodeScale = 2;
            //设置编码版本
            qrCodeEncoder.QRCodeVersion = 4;
            //设置编码错误纠正 
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

            Image image;
            //生成二维码图片
            image = qrCodeEncoder.Encode(data, Encoding.ASCII); //Encoding.UTF8);

            pictureBox2.Image = image;
        }

        //标签信息显示
        public void bqxx()
        {
            

        }

        //标签内容置空
        public void bqzk()
        {
            label50.Text = "";
            label16.Text = "";
            label7.Text = "";
            //label23.Text = "";
            //label37.Text = "";
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            //Bitmap _NewBitmap = new Bitmap(300, 300);
            //panel5.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            Pen p = new Pen(System.Drawing.Color.FromArgb(255, System.Drawing.Color.Black), 1); //线条颜色和粗细
            Pen pp = new Pen(System.Drawing.Color.Black, 0.3F);
            Font MyFont1 = new Font("宋体", 9);
            Font MyFont2 = new Font("宋体", 12,FontStyle.Bold);



            ////100*120
            //int xS = 10, xs1 = 40, yS = 5, h = 10, ys1 = 50, ys2 = 60;

            e.Graphics.PageUnit = GraphicsUnit.Millimeter;   //位置度量单位设为毫米
            e.Graphics.TranslateTransform(0F, 0F);  //偏移量
            ////二维码
            Bitmap _NewBitmap = new Bitmap(200, 200);
            pictureBox2.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            e.Graphics.DrawImage(_NewBitmap, 5, 5, 70, 70);
            //e.Graphics.DrawImage(pictureBox1.Image, 35, 20, 30, 10);
            //
            //e.Graphics.DrawString("宏格", MyFont2, Brushes.Black, 45, 5);
            //e.Graphics.DrawString("物料标识卡", MyFont2, Brushes.Black, 40, 12);
            ////画表格横线
            e.Graphics.DrawLine(pp, new Point(5, 32), new Point(75, 32));
            // e.Graphics.DrawLine(pp, new Point(5, 40-6), new Point(75, 40-6));
            //e.Graphics.DrawLine(pp, new Point(5, 45-4), new Point(75, 45-4));
            // e.Graphics.DrawLine(pp, new Point(5, 50-2), new Point(75, 50-2));
            // e.Graphics.DrawLine(pp, new Point(5, 55), new Point(75, 55));

            ////画表格竖线 
            // e.Graphics.DrawLine(pp, new Point(5, 35 - 8), new Point(5, 55));
            // e.Graphics.DrawLine(pp, new Point(20, 35 - 8), new Point(20, 55));
            // e.Graphics.DrawLine(pp, new Point(75, 35 - 8), new Point(75, 55));


            ////二维码编号
            e.Graphics.DrawString(label18.Text, MyFont2, Brushes.Black, 30, 5);
            //e.Graphics.DrawString(label17.Text, MyFont2, Brushes.Black, 30, 15);
            //e.Graphics.DrawString(label17.Text, MyFont1, Brushes.Black, 5, 28);
            e.Graphics.DrawString("物料编码:", MyFont1, Brushes.Black, 5, 33);
            e.Graphics.DrawString("物料名称:", MyFont1, Brushes.Black, 5, 38);
            e.Graphics.DrawString("物料规格:", MyFont1, Brushes.Black, 5, 43);
            e.Graphics.DrawString("入库日期:", MyFont1, Brushes.Black, 5, 48);
            e.Graphics.DrawString("入库单号:", MyFont1, Brushes.Black, 5, 53);
            //e.Graphics.DrawString("数量:", MyFont1, Brushes.Black, 5, 50 - 1);
            e.Graphics.DrawString(label50.Text, MyFont1, Brushes.Black, 20, 33);
            e.Graphics.DrawString(label16.Text, MyFont1, Brushes.Black, 20, 38);
            e.Graphics.DrawString(label7.Text, MyFont1, Brushes.Black, 20, 43);
            e.Graphics.DrawString(label13.Text, MyFont1, Brushes.Black, 20, 48);
            e.Graphics.DrawString(label15.Text, MyFont1, Brushes.Black, 20, 53);
           
            Font MyFont3 = new Font("宋体", 20, FontStyle.Bold);
            e.Graphics.DrawString("图号：" + label17.Text, MyFont3, Brushes.Black, 5, 55);
        }

        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {

        }

        private void FormT8BQ_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bdmain();
            bddetail();
        }


        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {
            label50.Text = textBox15.Text;
            ewmsc();

        }

        private void textBox17_TextChanged(object sender, EventArgs e)
        {
            label16.Text = textBox17.Text;
            ewmsc();
        }


        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            label13.Text = textBox18.Text;
            ewmsc();
        }


        private void textBox9_TextChanged(object sender, EventArgs e)
        {

            label18.Text = "包装数量：" + textBox9.Text;
            ewmsc();
        }

     

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            ewmsc();
            if (textBox9.Text == "") { MessageBox.Show("请输入包装数量！", "提示信息"); }
            else if (textBox19.Text == "") { MessageBox.Show("请选择打印份数！", "提示信息"); }
            else if (Convert.ToInt16(textBox19.Text) > 0)
            {
                for (int q1 = 0; q1 < Convert.ToInt16(textBox19.Text); q1++)
                {
                    //设置打印时横向还是纵向
                    //printDocument1.PrinterSettings.Copies = Convert.ToInt16(textBox10.Text);
                    printDocument1.DefaultPageSettings.Landscape = false;
                    printDocument1.DefaultPageSettings.Margins = new Margins(20, 20, 20, 0);
                    printDocument1.Print();
                    dbconn db = new dbconn();
                    //db.sqlcommand("update capMembership set cu_tmcs='" + (Convert.ToInt16(label46.Text) + 1).ToString() + "' where userid='" + label27.Text + "'");
                    ewmsc();
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label7.Text = textBox2.Text;
        }


        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            bddetail();
        }

   
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            bddetail();
        }

        private void dataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
               textBox11.Text=dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
               textBox18.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
               textBox15.Text = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
               textBox17.Text = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
               textBox2.Text = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
               label50.Text = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
               label16.Text = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
               label7.Text = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
               label17.Text = dataGridView2.SelectedRows[0].Cells[6].Value.ToString();
                ewmsc();
                bqxx();
            }

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            label15.Text = textBox11.Text;
        }

    }
}
